<?php

  #Try to make some sql query caching
  class sqlQueryCache{

  var $enabled    = true;
  var $enabledfor = array('one' => true,
			  'row' => true,
			  'all' => true
			  );

  function init(){
  	$_SESSION['sqlQueryCache'] = array();
  	$_SESSION['globalDbLog']   = array();
  }
  function doLog($sql){
	$_SESSION['globalDbLog'][] = $sql;
  }


  function addToCache($sql,$result){
		$in_cache = false;
		if(is_array($_SESSION['sqlQueryCache']))
		foreach($_SESSION['sqlQueryCache'] as $k => $v){
			if($k == $sql){
			$in_cache = true;
			}				
		}

		if($in_cache == false){
                  $_SESSION['sqlQueryCache'][$sql] = $result;

		}
  }

  function getFromCache($sql,$type){
		if($this->enabled == true && $this->enabledfor[$type] == true){

			$in_cache = false;
			if(is_array($_SESSION['sqlQueryCache']))
			foreach($_SESSION['sqlQueryCache'] as $k => $v){
				if($k == $sql){
				$in_cache = true;
				}				
			}
			
			if($in_cache == true){
                	  return $_SESSION['sqlQueryCache'][$sql];
			}
			else{
			  return false;
			}
		
		}
		else{
			return false;
		}


  }

  function showCache(){
		$_SESSION['CacheResultLog']['TOTALREQ']     = '<b>'.(count($_SESSION['globalDbLog'])).' requests </b>';
		$_SESSION['CacheResultLog']['UNIQUEREQ']    = '<b>'.(count($_SESSION['sqlQueryCache'])).' unique requests </b>';;		
		$all_log_count = (count($_SESSION['globalDbLog']));
		$cached_log_count = (count($_SESSION['sqlQueryCache']));

		$dublicate_percent =  round(((($all_log_count-$cached_log_count)/$all_log_count)*100));
	        $from_cache = count($_SESSION['CacheResultLog']['TAKENFROMCACHE']);
		$_SESSION['CacheResultLog']['DUBLICATEQTY'] = '<b>'.($all_log_count-$cached_log_count)."   ([$dublicate_percent%] dublicats) </b>";		

        	#debug($_SESSION['sqlQueryCache'],'UNIQUE CACHE REQUESTS');
        	#debug($_SESSION['globalDbLog'],'ALL REQUESTS');
		if($this->enabled){$enabled = 'ENABLED';}else{$enabled = 'DISABLED';}
        	debug($_SESSION['CacheResultLog'],'Summary | Caching is <u>'.$enabled.'</u>');
	}

}


?>